package work;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
@WebServlet("/dididi2")

//获取所有信息
public class servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
//        获取真实路径
        String realPath = context.getRealPath("stu.txt");
//        读取数据
        BufferedReader br = new BufferedReader(new FileReader(realPath));
//        处理乱码
        resp.setContentType("text/html;charset=utf-8");
        String s;
        while((s=br.readLine())!=null){
            resp.getWriter().write(s+"<br>");
        }
    }
}

